package com.zhangtai.modules.service.impl.employee;



import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhangtai.modules.dao.ReportBackInfoDao;
import com.zhangtai.modules.entity.employee.ReportBackInfoEntity;
import com.zhangtai.modules.entity.leave.ShiftEntity;
import com.zhangtai.modules.service.employee.ReportBackInfoService;
import com.zhangtai.utils.PageResult;
import com.zhangtai.utils.R;
import org.apache.commons.lang3.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;


@RestController
public class ReportBackInfoServiceImpl extends ServiceImpl<ReportBackInfoDao, ReportBackInfoEntity> implements ReportBackInfoService {

    @Autowired
    private ReportBackInfoDao reportBackInfoDao;

    @Override
    public PageResult queryPage(@RequestParam(value = "createtime", required = false) String createtime,
                                @RequestParam(value = "userName", required = false) String userName,
                                @RequestParam(value = "code", required = false) String code,
                                @RequestParam(value = "beginGmtCancel", required = false) String beginGmtCancel,
                                @RequestParam(value = "endGmtCancel", required = false) String endGmtCancel,
                                @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {

        IPage<ReportBackInfoEntity> page = this.page(
                new Page<ReportBackInfoEntity>(pageNum,pageSize),
                new QueryWrapper<ReportBackInfoEntity>()
                        .like(!StringUtils.isEmpty(userName),"user_name",userName)
                        .eq(!StringUtils.isEmpty(code),"code",code)
                        .eq(!StringUtils.isEmpty(createtime),"createTime",createtime)
                        .ge(!com.zhangtai.utils.StringUtils.isEmpty(beginGmtCancel),"shift_date",beginGmtCancel)
                        .le(!com.zhangtai.utils.StringUtils.isEmpty(endGmtCancel),"shift_date",endGmtCancel)
                        .eq("is_delete",0)
                        .orderByDesc("createTime")
        );


        return new PageResult(page);
    }

    @Override
    public PageResult<ReportBackInfoEntity> queryPageHr(
            @RequestParam(value = "orgName", required = false) String orgName,
            @RequestParam(value = "unitName", required = false) String unitName,
            @RequestParam(value = "jobName", required = false) String jobName,
            @RequestParam(value = "userName", required = false) String userName,
            @RequestParam(value = "createtime", required = false) String createtime,
                                @RequestParam(value = "orgIds", required = false) List<Long> orgIds,
                                @RequestParam(value = "beginGmtCancel", required = false) String beginGmtCancel,
                                @RequestParam(value = "endGmtCancel", required = false) String endGmtCancel,
                                @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {

        Page<ReportBackInfoEntity> page =  new Page<ReportBackInfoEntity>(pageNum,pageSize);

        List<ReportBackInfoEntity> reportAuth = reportBackInfoDao.getReportAuth(orgName,unitName,jobName,userName ,createtime
                , orgIds, beginGmtCancel
                , endGmtCancel, page);

        page.setRecords(reportAuth);
        return new PageResult<ReportBackInfoEntity>(page) ;
    }

    @Override
    public ReportBackInfoEntity getByIdE(Long id) {
        ReportBackInfoEntity one = this.getOne(new QueryWrapper<ReportBackInfoEntity>().eq("id", id).eq("is_delete", 0));

        return one;
    }

    @Override
    public R saveE(ReportBackInfoEntity entity) {
        boolean b = this.save(entity);
        if (!b){
            return R.error();
        }
        return R.ok();

    }

    @Override
    public R updateByIdE(ReportBackInfoEntity entity) {
        boolean b = this.updateById(entity);
        if (!b){
            return R.error();
        }
        return R.ok();
    }

    @Override
    public R removeOneById(Long id) {
        ReportBackInfoEntity entity = new ReportBackInfoEntity();
        entity.setId(id);
        entity.setIsDelete(1);
        boolean b = this.updateById(entity);
        if (!b){
            return R.error();
        }
        return R.ok();
    }

    @Override
    @Transactional
    public R removeByIdsE(List<Long> ids) {
        for (Long id : ids) {
            R r = removeOneById(id);
            if (!r.getFlag()){
                return r;
            }
        }
        return R.ok();
    }

}